﻿function Save(CaseType, CaseIndex) {
    DBNo = document.getElementById("divDBNo").value;
    Payer = document.getElementById("divPayer").value;
    Subject = document.getElementById("divSubject").value;
    Attention = document.getElementById("divAttention").value;
    CurrencyIndex = document.getElementById("divCurrency").selectedIndex;
    Currency = document.getElementById("divCurrency").options[CurrencyIndex].value;
    Signer = document.getElementById("divSigner").value;
    Title = document.getElementById("divTitle").value;
    thueVat = document.getElementById("divPercentVat").value;
    DebitNotePrintAccountIndex = document.getElementById("divSelectAccount").selectedIndex;
    DebitNotePrintAccountId = document.getElementById("divSelectAccount").options[DebitNotePrintAccountIndex].value;
    fDate = document.getElementById("divCreateDate").value;

    if (document.getElementById("divCaseType") != null) {
        CaseType = document.getElementById("divCaseType").innerHTML;
        CaseIndex = document.getElementById("divCaseIndex").value;
    }


    EnglishAccount = document.getElementById("divAccountEnglish").value;
    VietnameseAccount = document.getElementById("divAccountVietnamese").value;


    ShortName = document.getElementById("txtShortCustomerName").value;
    CustomerName = document.getElementById("txtCustomerName").value;
    Address = document.getElementById("txtCustomerAddress").value;
    Phone = document.getElementById("txtCustomerPhone").value;

    pType = document.getElementById("drpSelectPType").options[document.getElementById("drpSelectPType").selectedIndex].value;
    fromLocationId = document.getElementById("selectFromDepartment").options[document.getElementById("selectFromDepartment").selectedIndex].value;
    toLocationId = document.getElementById("selectToDepartment").options[document.getElementById("selectToDepartment").selectedIndex].value;

    Max = 12;
    SaveItemData = "";
    for (var iIndex = 0; iIndex < Max; iIndex++) {
        Item = document.getElementById("txtIndex_" + iIndex).value;
        Description = document.getElementById("txtDescription_" + iIndex).value;
        Fee = document.getElementById("txtProFee_" + iIndex).value;
        NOIP = document.getElementById("txtOffFee_" + iIndex).value;
        Postage = document.getElementById("txtOther_" + iIndex).value;
        PercentDiscount = document.getElementById("txtDiscount_" + iIndex).value;
        InternalSharing = document.getElementById("txtInternalSharing_" + iIndex).value;

        SaveItemData = SaveItemData + Description;
        SaveItemData = SaveItemData + "~" + Fee;
        SaveItemData = SaveItemData + "~" + NOIP;
        SaveItemData = SaveItemData + "~" + Postage;
        SaveItemData = SaveItemData + "~" + PercentDiscount;
        SaveItemData = SaveItemData + "~" + Item;
        SaveItemData = SaveItemData + "~" + InternalSharing;
        if (iIndex < Max - 1) {
            SaveItemData = SaveItemData + "|";
        }
    }

    DBNIndex = Portal.Vision.AddQuotation.ServerSideAddQuotation(CaseType, CaseIndex, fDate, DebitNotePrintAccountId, DBNo, Payer, Subject, Attention, Currency, Signer, Title, EnglishAccount, VietnameseAccount, thueVat, ShortName, CustomerName, Address, Phone, pType, fromLocationId, toLocationId, SaveItemData).value;
    if (DBNIndex != null) {
        if (DBNIndex.indexOf("ERROR") != -1) {
            alert(DBNIndex);
            return;
        }
    }

    Url = Portal.Vision.AddQuotation.ServerSideGetViewUrl(DBNIndex).value;
    alert("Phát hành chào giá thành công!");
    window.open(Url, "_self");
}

function ClientDetectDebitNoFunc() {
    CaseIndex = document.getElementById("divCaseIndex").value;
    CaseType = document.getElementById("divCaseType").innerHTML;
    RetValue = Portal.Vision.AddQuotation.GetDebitNodeNo(CaseType, CaseIndex).value;
    if (RetValue != null) {
        if (RetValue.indexOf("ERROR") != -1) {
            alert(RetValue);
            return;
        }
    }
    document.getElementById("divDBNo").value = RetValue;
}


function DetectAccountFunc() {
    selectedIndex = document.getElementById("divSelectAccount").selectedIndex;
    Id = document.getElementById("divSelectAccount").options[selectedIndex].value;
    Vietnamese = Portal.Vision.AddQuotation.GetVietnamese(Id).value;
    English = Portal.Vision.AddQuotation.GetEnglish(Id).value;
    document.getElementById("divAccountEnglish").value = English;
    document.getElementById("divAccountVietnamese").value = Vietnamese;
}

function DetectSignerTitle() {
    Signer = document.getElementById("divSigner").value;
    RetValue = Portal.Vision.AddQuotation.ServerSideDetectSignerTitle(Signer).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }
    document.getElementById("divTitle").value = RetValue;
}


function ClientDetectDebitNoFunc() {
    CaseIndex = document.getElementById("divCaseIndex").value;
    CaseType = document.getElementById("divCaseType").innerHTML;
    RetValue = Portal.Vision.AddQuotation.GetDebitNodeNo(CaseType, CaseIndex).value;
    if (RetValue != null) {
        if (RetValue.indexOf("ERROR") != -1) {
            alert(RetValue);
            return;
        }
    }
    document.getElementById("divDBNo").value = RetValue;
}


function DetectAccountFunc() {
    selectedIndex = document.getElementById("divSelectAccount").selectedIndex;
    Id = document.getElementById("divSelectAccount").options[selectedIndex].value;
    Vietnamese = Portal.Vision.AddQuotation.GetVietnamese(Id).value;
    English = Portal.Vision.AddQuotation.GetEnglish(Id).value;
    document.getElementById("divAccountEnglish").value = English;
    document.getElementById("divAccountVietnamese").value = Vietnamese;
}

function DetectSignerTitle() {
    Signer = document.getElementById("divSigner").value;
    RetValue = Portal.Vision.AddQuotation.ServerSideDetectSignerTitle(Signer).value;
    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue);
        return;
    }
    document.getElementById("divTitle").value = RetValue;
}


function DetectOriginCustomer() {
    CustIndex = document.getElementById("divCustIndex").value;
    ShortName = Portal.Vision.AddQuotation.ServerGetCustomerInfo(CustIndex, "ShortName").value;
    if (ShortName.indexOf("ERROR") != -1) {
        alert(ShortName);
        return;
    }

    document.getElementById("txtShortCustomerName").value = ShortName;

    CustName = Portal.Vision.AddQuotation.ServerGetCustomerInfo(CustIndex, "CustName").value;
    if (CustName.indexOf("ERROR") != -1) {
        alert(CustName);
        return;
    }

    document.getElementById("txtCustomerName").value = CustName;

    Address = Portal.Vision.AddQuotation.ServerGetCustomerInfo(CustIndex, "Address").value;
    if (Address.indexOf("ERROR") != -1) {
        alert(Address);
        return;
    }
    document.getElementById("txtCustomerAddress").value = Address;


    Phone = Portal.Vision.AddQuotation.ServerGetCustomerInfo(CustIndex, "Phone").value;
    if (Phone.indexOf("ERROR") != -1) {
        alert(Address);
        return;
    }
    document.getElementById("txtCustomerPhone").value = Phone;
}